var App = getApp();
var ApiUrl = App.data.ApiUrl;
var Common = require('../../../utils/common.js');
Page({
    data: {
        imageSize: [],
        last_rec_id: 0,
        result: 0,
        search: '',
        list_main: []
    },
    onLoad: function (options) {
        var page = this;
        Common.w_widths(page);
        wx.setNavigationBarTitle({
            title: '中奖查询'
        })

        var page = this;
        if (options) {/**预制顾问二维码 */
            /** 扫码出来的有scene */
            if (options.scene) {
                var scene = decodeURIComponent(options.scene);
                console.log("scene:");
                console.log(scene);
                /*将scene转成JSON*/
                var datajson = {};
                var sj = scene.split('&');
                for (var i in sj) {
                    var sjj = sj[i].split('=');
                    datajson[sjj[0]] = sjj[1];
                }
            } else {/**普通链接 */
                console.log("普通链接");
                var datajson = options;
            }
            console.log("datajson");
            console.log(datajson);
            if (datajson.id) {
                page.setData({
                    activity_id: datajson.id
                });
            }
        }
        this.refresh()
    },
    imageLoad: function (e) {
        var page = this,
            id = e.currentTarget.id;
        Common.imageLoadList(e, page);
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },
    // 更新券号/店名
    updatesearch: function (e) {
        this.setData({
            search: e.detail.value
        })
    },
    refresh: function () {
        wx.showLoading({
            title: '加载中...',
        })
        var page = this,
            activity_id = page.data.activity_id,
            search = page.data.search,
            last_rec_id = page.data.last_rec_id;
        Common.my_request({
            url: 'AppMaster/LuckyDraw/getWinningList',
            data: {
                activity_id: activity_id,
                search: search,
                last_rec_id: last_rec_id,
                rows: 20
            },
            success: function (res) {
                if (res.data.result == 0) {
                    if (res.data.data.length > 0) {
                        var newData = page.data.list_main,
                            Lists = res.data.data
                        if (page.data.lowers) {
                            newData = newData.concat(Lists)
                        } else {
                            newData = Lists
                        }
                        page.setData({
                            list_data: res.data,
                            list_main: newData,
                            result: 1,
                            msg: '加载中...',
                            last_rec_id: res.data.data[res.data.data.length - 1].rec_id
                        })
                        if (!Lists || Lists.length < 20) {
                            page.setData({
                                msg: '没有更多了',
                            })
                        }
                    } else {
                        if (page.data.list_main.length > 0) {
                            page.setData({
                                msg: '没有更多了',
                            })
                        } else {
                            page.setData({
                                msg: '没有中奖哦',
                            })
                            wx.showToast({
                                title: '没有找到中奖纪录！',
                                image: '../../images/fail.png',
                                duration: 2000
                            })
                        }
                    }
                    // 关闭加载
                    wx.hideLoading();

                } else {
                    // 关闭加载
                    wx.hideLoading();
                    wx.showToast({
                        title: res.data.message,
                        image: '../../images/fail.png',
                        duration: 2000
                    })
                }

            }
        });
    },
    lower: function () {
        this.setData({
            lowers: true,
        })
        this.refresh()
    },
    newrefresh: function () {
        this.setData({
            lowers: false,
            last_rec_id: 0,
            list_main: [],
            scrollTop: 0
        })
        this.refresh()
    },
    details: function (e) {
        var itemId = e.currentTarget.id,
            list_main = this.data.list_main;
        this.setData({
            current: list_main[itemId],
            result: 2,
        })
    }
})